package com.market.goods.web;

import com.alibaba.fastjson.JSON;
import com.market.goods.constance.SuperRandom;
import com.market.goods.dao.es.Location;
import com.market.goods.dao.es.MarketGoodsDocument;
import com.market.goods.dao.mongo.MarketGoodsDetails;
import com.market.goods.dao.po.MarketGoodsPO;
import com.market.goods.producer.KafkaClusterProducer;
import com.market.goods.service.MarketGoodsService;
import com.market.goods.service.MongoDbService;
import com.market.goods.web.vo.QueryMarketGoodsDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Api(tags = "mongodb相关服务")
@RestController
@RequestMapping("/mongodb")
public class MongodbController {

    @Autowired
    MongoDbService mongoDbService;

    @Autowired
    MarketGoodsService marketGoodsService;

    @Autowired
    KafkaClusterProducer kafkaClusterProducer;

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(6, 12, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(300));

    @ApiOperation("mock数据")
    @GetMapping("mongodbMockData")
    public String mongodbMockData() {
        for (int k = 0; k < 6; k++) {
            CompletableFuture.runAsync(() -> {
                saveObj();
            }, threadPoolExecutor);
        }
        return "触发写入";
    }


    @ApiOperation("根据goodsId获取商品")
    @GetMapping("getMarketGoodsById")
    public MarketGoodsDetails getMarketGoodsById(String goodsId) {
        return mongoDbService.getMarketGoodsById(goodsId);
    }

    @ApiOperation("查询门店所有商品")
    @GetMapping("getMarketGoodsListByShopId")
    public List<MarketGoodsDetails> getMarketGoodsListByShopId(String shopId) {
        return mongoDbService.getMarketGoodsListByShopId(shopId);
    }

    @ApiOperation("查询商品分页接口")
    @PostMapping("getPageMarketGoodsList")
    public List<MarketGoodsDetails> getPageMarketGoodsList(QueryMarketGoodsDetailsVO queryMarketGoodsDetailsVO) {
        return mongoDbService.getPageMarketGoodsList(queryMarketGoodsDetailsVO);
    }

    private void saveObj() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            for (int k = 0; k < 8000000; k++) {
                String shopName = SuperRandom.getShopName();

                //纬度
                int latitudeZhengshu = (int) (Math.random() * (53 - 3 + 1) + 3);
                int latitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);
                //经度
                int longitudeZhengshu = (int) (Math.random() * (135 - 73 + 1) + 73);
                int longitudeXiaoshu = (int) (Math.random() * (100 - 0 + 1) + 0);


                String longitude = "";
//                if (longitudeZhengshu % 3 == 0) {
                longitude = longitudeZhengshu + "." + longitudeXiaoshu;
//                } else {
//                    longitude = "-" + longitudeZhengshu + "." + longitudeXiaoshu;
//                }
                String latitude = latitudeZhengshu + "." + latitudeXiaoshu;
                int createTime = SuperRandom.getNum(2017, 2020);
                Date time = SuperRandom.getTime(createTime);
//            String format = formatter.format(time);
//            log.info("addFormat={}",format);
                MarketGoodsPO marketGoodsPO = new MarketGoodsPO();
                marketGoodsPO.setShopId(UUID.randomUUID().toString().replace("-", "") + SuperRandom.idWorker.nextId());
                marketGoodsPO.setCreatedTime(time);
                marketGoodsPO.setUpdateTime(time);
                marketGoodsPO.setLatitude(new BigDecimal(latitude));
                marketGoodsPO.setLongitude(new BigDecimal(longitude));
                marketGoodsPO.setShopName(shopName);
                marketGoodsPO.setShopScoring(SuperRandom.getNum(1, 10));
                marketGoodsPO.setShopGrade(SuperRandom.getNum(1, 3));
                int randomnum = SuperRandom.getNum(12, 22);
                for (int j = 0; j < randomnum; j++) {
                    int zhengshu = (int) (Math.random() * (10000 + 1));
                    int xiaoshu = (int) (Math.random() * (99 - 0 + 1) + 0);
                    String goodsPrice = zhengshu + "." + xiaoshu;
                    long goodsTypeId = (long) (Math.random() * (999999999 - 0 + 1) + 0);
                    marketGoodsPO.setGoodsTypeId(goodsTypeId);
                    marketGoodsPO.setGoodsId(UUID.randomUUID().toString().replace("-", ""));
                    marketGoodsPO.setVersion(1);
                    String goodsName = SuperRandom.getCaiName();
                    String goodsLabels = SuperRandom.getGoodsLabels();
                    marketGoodsPO.setGoodsName(goodsName);
                    marketGoodsPO.setGoodsLabels(goodsLabels);
                    marketGoodsPO.setGoodsScoring(SuperRandom.getNum(1, 10));
                    marketGoodsPO.setGoodsGrade(SuperRandom.getNum(1, 3));
                    MarketGoodsDocument marketGoodsDocument = new MarketGoodsDocument();
                    MarketGoodsDetails marketGoodsDetails = new MarketGoodsDetails();
                    BeanUtils.copyProperties(marketGoodsPO, marketGoodsDocument);
                    BeanUtils.copyProperties(marketGoodsPO, marketGoodsDetails);
                    Location location = new Location();
                    location.setLat(marketGoodsPO.getLatitude());
                    location.setLon(marketGoodsPO.getLongitude());
//                List<BigDecimal> location = Lists.newArrayList();
//                location.add(marketGoodsPO.getLatitude());
//                location.add(marketGoodsPO.getLongitude());
                    marketGoodsDocument.setLocation(location);
                    marketGoodsDetails.setLocation(location);
                    String goodsId = SuperRandom.idWorker.nextId() + UUID.randomUUID().toString().replace("-", "");
                    marketGoodsDetails.setGoodsId(goodsId);
                    marketGoodsDetails.setGoodsId(goodsId);
//                    marketGoodsDetails.setGoodsPrice(new BigDecimal(goodsPrice));
                    BeanUtils.copyProperties(marketGoodsDetails, marketGoodsDocument);
//                    marketGoodsService.insertEs(marketGoodsDocument);
                    String message = JSON.toJSONString(marketGoodsDocument);
                    kafkaClusterProducer.sendToKafkaStandardMessageAsync("async-topic",message);
//                    mongoDbService.insertMarketGoodsDetailsForMongodb(marketGoodsDetails);
//                    log.info("MarketGoodsControllerMarketGoodsDocument={}", marketGoodsDocument);
//                    marketGoodsService.insertEs(marketGoodsDocument);
//                    marketGoodsService.insertSelective(marketGoodsPO);
                }
            }
        } catch (Exception e) {
            log.info("insertSelectiveException={}", e);
        }
    }

}
